/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AstroIMRoster.java
 *
 * Created on 11-Jul-2010, 18:50:38
 */
package astroim;

import astroim.smack.Connection;
import astroim.smack.CustomChatListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

/**
 *
 * @author alykhalid
 */
public class AstroIMRoster extends javax.swing.JPanel {

    /** Creates new form AstroIMRoster */
    public AstroIMRoster() {
        initComponents();
    }

    public void DisplayRoster() {
        try {
            Connection conn = Connection.getInstance();
            jList1.setModel(conn.Connect().Roster().ContactsOnline);
            jList1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            jList1.setLayoutOrientation(JList.HORIZONTAL_WRAP);
            jList1.setVisibleRowCount(-1);
        } catch (XMPPException ex) {
            Logger.getLogger(AstroIMRoster.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setName("Form"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Available", "Away", "Busy", "Offline" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setName("jList1"); // NOI18N
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 335, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        String JID = jList1.getSelectedValue().toString();
        if (CustomChatListener.getInstance().getChatters().containsKey(JID)) {
            CustomChatListener.getInstance().getChatters().get(JID).ProcessChat(JID);
        } else {
            CustomChatListener.getInstance().getChatters().put(JID, new AstroIMMessaging().ProcessChat(JID));
        }
            
    }//GEN-LAST:event_jList1MouseClicked

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        XMPPConnection conn = Connection.getInstance().getConnection();

        Presence presence = new Presence(Presence.Type.available);
        presence.setPriority(24);

        if("Available".equalsIgnoreCase(jComboBox1.getSelectedItem().toString())){
             if(!conn.isConnected())
               DisplayRoster();
            presence.setMode(Presence.Mode.available);
            conn.sendPacket(presence);
        } else if("Away".equalsIgnoreCase(jComboBox1.getSelectedItem().toString())){
            presence.setMode(Presence.Mode.away);
            conn.sendPacket(presence);
        }else if("Busy".equalsIgnoreCase(jComboBox1.getSelectedItem().toString())){
            presence.setMode(Presence.Mode.dnd);
            conn.sendPacket(presence);
        }else if("Offline".equalsIgnoreCase(jComboBox1.getSelectedItem().toString())){
            conn.disconnect();
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
